package data.base;

import java.awt.Color;

public class RenderColor {
  protected double red;
  protected double green;
  protected double blue;
  
  public RenderColor(double brightness){
    red = brightness;
    green = brightness;
    blue = brightness;
  }
  
  public RenderColor(double red, double green, double blue){
    this.red = red;
    this.green = green;
    this.blue = blue;
  }
  
  public RenderColor(Color c){
    red = c.getRed() / 255d;
    green = c.getGreen() / 255d;
    blue = c.getBlue() / 255d;
  }
  
  public RenderColor(int bits){
    red = ((bits >> 16) & 0xFF) / 255d;
    green = ((bits >> 8) & 0xFF) / 255d;
    blue = (bits & 0xFF) / 255d;
  }
  
  public void mixWithColor(RenderColor c, double balance){
    red = red * (1 - balance) + c.red * balance;
    green = green * (1 - balance) + c.green * balance;
    blue = blue * (1 - balance) + c.blue * balance;
  }
  
  public void multiplyColor(RenderColor c){
    red *= c.red;
    green *= c.green;
    blue *= c.blue;
  }
  
  public void addColor(RenderColor c){
    red += c.red;
    green += c.green;
    blue += c.blue;
  }
  
  public void addColor(int bits){
    red += ((bits >> 16) & 0xFF) / 255d;
    green += ((bits >> 8) & 0xFF) / 255d;
    blue += (bits & 0xFF) / 255d;
  }
  
  public void multiplyBrightness(double factor){
    red *= factor;
    green *= factor;
    blue *= factor;
  }
  
  public Color getColor(){
    float r = Math.max(0, Math.min(1, (float)red));
    float g = Math.max(0, Math.min(1, (float)green));
    float b = Math.max(0, Math.min(1, (float)blue));
    return new Color(r, g, b);
  }
  
  public RenderColor clone(){
    return new RenderColor(red, green, blue);
  }
  
  @Override
  public boolean equals(Object o){
    if(o instanceof RenderColor){
      RenderColor c = (RenderColor)o;
      if(c.red == red && c.green == green && c.blue == blue)
        return true;
    }
    return false;
  }
  
  public double getDifferenceTo(RenderColor c){
    return (Math.abs(red - c.red) + Math.abs(green - c.green) + Math.abs(blue - c.blue)) / 3;
  }
}
